/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ajustes;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jessica Ordoñez
 */
public class Conexion2 {

    private Connection conx;

    public Conexion2() {

        try {
            // Load the jdbc driver.
            Class.forName("com.sybase.jdbc4.jdbc.SybDataSource");
            //Class drvClass = Class.forName("com.sybase.jdbc4.jdbc.SybDataSource");
            //DriverManager.registerDriver((Driver) drvClass.newInstance());
            //DriverManager.setLoginTimeout(900);		//15 minute timeout

            // Attempt to connect to a driver.

            conx = DriverManager.getConnection("jdbc:sybase:Tds:localhost:2638?ServiceName=local",
                    "PRG", "693194986");

        } catch (ClassNotFoundException ex) {
            System.exit(1);

        } catch (SQLException ex) {
            System.exit(1);
        }

    }

    public Connection getConnection() {
        return conx;
    }

    public List<String[]> llenarComboDivs(JFAjustes aj) {

        Statement stmt;
        ResultSet rs;
        List<String[]> retVal = new ArrayList<String[]>();

        try {

            stmt = conx.createStatement();
            rs = stmt.executeQuery("SELECT D.NOMBRE, P.DBNAME, P.IPSTR "
                    + " FROM DIVISIONES D INNER JOIN PARAMETROS_CONEXIONES P "
                    + " ON D.PLUDIVISION = P.PLUDIVISION");
            aj.cboDivisiones.removeAllItems();

            while (rs.next()) {
              //  System.out.println("ID: " + rs.getString(1));
                aj.cboDivisiones.addItem(rs.getString(1));
                retVal.add(new String[]{rs.getString(1), rs.getString(2), rs.getString(3)});

            }

            rs.close();
            stmt.close();
            conx.close();

        } // end try
        catch (SQLException e) {
        }

        return retVal;
    }

    public List<String[]> llenarComboEmp(JFAjustes aj) {
        Statement stmt;
        ResultSet rs;
        List<String[]> retEmp = new ArrayList<String[]>();

        try {

            stmt = conx.createStatement();
            rs = stmt.executeQuery("SELECT NOMBRE, EMPRESAID FROM EMPRESAS");
            aj.cboEmpresas.removeAllItems();

            while (rs.next()) {
              //  System.out.println("ID: " + rs.getString(1));
                aj.cboEmpresas.addItem(rs.getString(1));
                retEmp.add(new String[]{rs.getString(1), rs.getString(2)});
                

            }

            rs.close();
            stmt.close();
            conx.close();

        } // end try
        catch (SQLException e) {
        }

        return retEmp;


    }
}




